package com.evolved.automata.experimental;
import java.io.*;
import java.util.*;

import com.evolved.automata.filetools.StandardTools;

public class SerializerAdapter {
	String j_UniqueName;
	private String j_ObjectDelimiter="$$";
	HashMap<String,Serializer> j_TypeToSerializerMap;
	
	int[] j_LocalData;
	Serializer j_PartialImplementation;
	
	public SerializerAdapter(Serializer serializer)
	{
		j_TypeToSerializerMap = new HashMap<String, Serializer>();
		j_PartialImplementation=serializer;
	}
	
	public void SetUniqueName(String uniqueName)
	{
		j_UniqueName=uniqueName;
	}
	
	public void Load(String fileFullName)
	{
		try
		{
			BufferedReader reader = new BufferedReader(new FileReader(fileFullName));
			String inputLine=reader.readLine();
			String[] name_ = inputLine.split("\\:");
			j_PartialImplementation.SetName(name_[0]);
			Load(reader);
			reader.close();
		}
		catch (Exception e)
		{
			java.io.StringWriter traceText = new java.io.StringWriter();
			java.io.PrintWriter pWriter = new java.io.PrintWriter(traceText,true);
			e.printStackTrace(pWriter);
			pWriter.close();
			throw new RuntimeException(traceText.toString());
		}
		
	}
	
	public void Load(BufferedReader reader)
	{
		String lineinput=null;
		String[] name_data;
		String name,data;
		String[] descendent_names;
		String typeName, instanceId;
		Serializer inner;
		j_PartialImplementation.PrepareSerialization();
		try
		{
			
			while ((lineinput!=null)||((lineinput=reader.readLine())!=null))
			{
				if (j_ObjectDelimiter.equals(lineinput))
					return;
				if (lineinput.equals(":"))
				{
					name="";
					data="";
				}
				else
				{
					name_data=StaticTools.SplitData(lineinput);
					name=name_data[0];
					if (name_data.length==2)
						data=name_data[1];
					else
						data="";
				}
				if (name.length()>j_UniqueName.length())
				{
					descendent_names=name.substring(j_UniqueName.length()+1).split("\\.");
					typeName=descendent_names[0];
					instanceId=descendent_names[1];
					
					inner=StaticTools.GetSerializer(typeName);
					inner.SetName(name);
					inner.Load(reader);
					j_PartialImplementation.AddData(inner, instanceId);
				}
				else
				{
					j_PartialImplementation.AddData(data);
				}
				lineinput=null;
			}
		}
		catch (Exception e)
		{
			java.io.StringWriter traceText = new java.io.StringWriter();
			java.io.PrintWriter pWriter = new java.io.PrintWriter(traceText,true);
			e.printStackTrace(pWriter);
			pWriter.close();
			throw new RuntimeException(traceText.toString());
		}
	}
	
	public void Save(String fileFullName)
	{
		try
		{
			if (StandardTools.FileExists(fileFullName))
				StandardTools.DeleteFile(fileFullName);
			BufferedWriter writer = new BufferedWriter(new FileWriter(fileFullName));
			Save(writer);
			writer.close();
		}
		catch (Exception e)
		{
			java.io.StringWriter traceText = new java.io.StringWriter();
			java.io.PrintWriter pWriter = new java.io.PrintWriter(traceText,true);
			e.printStackTrace(pWriter);
			pWriter.close();
			throw new RuntimeException(traceText.toString());
		}
	}
	
	public  void Save(BufferedWriter writer) 
	{
		
		try
		{
			
			writer.write(j_UniqueName+":");
			writer.newLine();
			j_PartialImplementation.SaveData(writer);
			writer.write(j_ObjectDelimiter);
			writer.newLine();
		}
		catch (Exception e)
		{
			java.io.StringWriter traceText = new java.io.StringWriter();
			java.io.PrintWriter pWriter = new java.io.PrintWriter(traceText,true);
			e.printStackTrace(pWriter);
			pWriter.close();
			throw new RuntimeException(traceText.toString());
		}
	}

	public String ConstructUniqueName(String typeId, String instanceId)
	{
		return String.format("%1$s.%2$s.%3$s", j_UniqueName,typeId,instanceId);
	}
	
	
	
	
}
